{include file="Public/header"/}
<div class="container" id="main">
    <div class="row">
        <div class="col-md-9">
            <!--- Article-Head -->
            <div class="article-head">
                <ol class="breadcrumb">
                    <li><a href="http://www.ldstars.com" rel="nofollow">首页</a></li>
                    <li><a href="http://www.ldstars.com/program">编程设计</a><a href="http://www.ldstars.com/program/c">C/C++</a>
                    </li>
                    <li class="active">正文</li>
                </ol>
                <!-- Blog-Box -->
                <h1 class="article-title text-center">利用localtime_s函数格式化输出当地日期与时间</h1>

                <div class="article-info">
                    <div class="col-md-4 col-xs-7">
                        <span class="article-author navy"><b>King</b></span>
                        <span class="article-time">发表于：&nbsp;2016-09-19</span>
                    </div>

                    <div class="col-md-4 col-md-offset-4 col-xs-5">
                        <span class="article-view">阅读:<span class="article-count red">2,143</span></span>
                        <span class="article-view">评论:<span class="article-count"><a
                                href="http://www.ldstars.com/program/c/311.html#comments">1 </a></span></span></div>
                </div>
            </div>
            <!--- //Article-Head -->

            <section class="article">
                <!--- Article-content -->
                <div class="article-content">
                    <p>Visual C++ 6.0开发环境中显示当地日期与时间主要通过<code>localtime()</code>函数来实现，该函数的原型在<code>time.h</code>头文件中，其语法格式如下：
                    </p>
                    <pre class="pure-highlightjs"><code class="">struct tm *localtime(xonst time_t *timer)</code></pre>
                    <p>该函数的作用是把timer所指的时间（如函数time返回的时间）转换为当地标准时间，并以tm结构形式返回。其中，参数timer为主要获取当前时间的传递参数，格式为time_t指针类型。</p>
                    <p>而在Visual Studio 2010极其以后的版本，新增了安全函数，改成<code>localtime_s()</code>，语法格式也发生了变化：</p>
                    <pre class="pure-highlightjs"><code class="">
errno_t localtime_s(
   struct tm* _tm,
   const time_t *time
);
                    </code></pre>
                    <p>其中：</p>
                    <pre class="pure-highlightjs">
                        <code class="">
_tm 指向要填充的时间结构的指针。
time 指针，指向存储的时间。
                        </code>
                    </pre>
                    <p><span id="mt5" class="sentence" data-guid="ea0da08be6ddf6b97383e5548c028da1"
                             data-source="Zero if successful.">如果成功，返回值则为零。</span>
                        <span id="mt6" class="sentence" data-guid="a3ee70d21f32499abb0babb4241aecc3"  data-source=" The return value is an error code if there is a failure."> 如果失败，返回值将是错误代码。</span>
                        <span id="mt7" class="sentence SentenceHover" data-guid="f6c8239a9e89c162594e4a8ebb4ee1ce" data-source=" Error codes are defined in Errno.h."> 错误代码是在 Errno.h 中定义的。</span></p>
                    <p>结构类型的字段 tm 存储下面的值，其中每个为 int。</p>
                    <pre class="pure-highlightjs"><code class="">
tm_sec
分钟后的几秒 (0-59)。
tm_min
小时后的分钟 (0-59)。
tm_hour
午夜后经过的小时 (0-23)。
tm_mday
月 (1-31) 天。
tm_mon
月 (0 – 11;年 1 月 = 0）。
tm_year
年份 （当前年份减去 1900年）。
tm_wday
星期几 (0 – 6;星期日 = 0）。
tm_yday
每年的一天 (0-365;1 月 1 日 = 0)。
tm_isdst
如果夏令时有效，则为，正值夏时制不起作用; 如果为 0如果夏时制的状态是未知的负值。 如果 TZ 设置环境变量，C 运行库会假定规则适用于美国境内用于实现夏令时 (DST) 计算。</code></pre>
                    <p>下面以一个实例来输出当地日期与时间：</p>
                    <pre class="pure-highlightjs">
                        <code class="">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;time.h&gt;

int main(void)
{
    struct tm t;   //tm结构指针
    time_t now;  //声明time_t类型变量
    time(&amp;now);      //获取系统日期和时间
    localtime_s(&amp;t, &amp;now);   //获取当地日期和时间
    //格式化输出本地时间
    printf("年：%d\n", t.tm_year + 1900);
    printf("月：%d\n", t.tm_mon + 1);
    printf("日：%d\n", t.tm_mday);
    printf("周：%d\n", t.tm_wday);
    printf("一年中：%d\n", t.tm_yday);
    printf("时：%d\n", t.tm_hour);
    printf("分：%d\n", t.tm_min);
    printf("秒：%d\n", t.tm_sec);
    printf("夏令时：%d\n", t.tm_isdst);
    system("pause");
    //getchar();
    return 0;

}
                        </code>
                    </pre>
                    <p>&nbsp;</p>
                </div>
                <div class="award">
                    <p><a href="javascript:void(0)" id="award" title="打赏，支持一下">赏</a></p>
                </div>
                <div class="award_box">
                    <div class="award_payimg">
                        <img src="http://www.ldstars.com/wp-content/themes/bulletin/img/alipayimg.jpg" alt="扫码支持" title="扫一扫"/>
                    </div>
                    <div class="pay_explain">扫码打赏，你说多少就多少</div>
                    <div class="award_payselect">
                        <div class="pay_item checked" data-id="alipay">
                            <span class="radiobox"></span>
                            <span class="pay_logo"><img src="http://www.ldstars.com/wp-content/themes/bulletin/img/alipay.jpg" alt="支付宝"/></span>
                        </div>

                        <div class="pay_item" data-id="weipay">
                            <span class="radiobox"></span>
                            <span class="pay_logo"><img src="http://www.ldstars.com/wp-content/themes/bulletin/img/wechat.jpg" alt="微信"/></span>
                        </div>
                    </div>
                    <div class="award_info">
                        <p>打开<span id="award_pay_txt">支付宝</span>扫一扫，即可进行扫码打赏哦</p>
                    </div>
                </div>
                <div class="article-foot">
                    <div class="article-foot-l pull-left">
                        <h3>标签：</h3>
                        <div class="article-tags">
                            <a href="http://www.ldstars.com/tag/c%e8%af%ad%e8%a8%80" rel="tag">C语言</a><a href="http://www.ldstars.com/tag/%e6%95%99%e7%a8%8b" rel="tag">教程</a>
                        </div>
                        <h3>分享到：</h3>
                        <div class="bdsharebuttonbox">
                            <a class="bds_qzone" onclick="javascript:bShare.share(event,'qzone',0);return false;" title="分享到QQ空间"></a>
                            <a class="bds_tsina" onclick="javascript:bShare.share(event,'sinaminiblog',0);return false;" title="分享到新浪微博"></a>
                            <a class="bds_weixin" onclick="javascript:bShare.share(event,'weixin',0);return false;" title="分享到微信"></a><a class="bds_tqq" onclick="javascript:bShare.share(event,'qqmb',0);return false;" title="分享到腾讯微博"></a>
                            <a class="bds_sqq" onclick="javascript:bShare.share(event,'qqim',0);return false;"
                               title="分享到QQ好友"></a>
                            <a class="bds_douban" onclick="javascript:bShare.share(event,'douban',0);return false;" title="分享到豆瓣"></a>
                            <a class="bds_copy" onclick="javascript:bShare.share(event,'clipboard',0);return false;"
                               title="分享到复制网址"></a>
                            <a class="bds_mail" onclick="javascript:bShare.share(event,'email',0);return false;"
                               title="分享到邮件">
                            </a>
                        </div>
                        <script type="text/javascript" charset="utf-8"
                                src="http://static.bshare.cn/b/buttonLite.js#uuid=&style=-1"></script>
                    </div>

                    <div class="article-qcode pull-right text-center">
                        <img src="http://qr.liantu.com/api.php?&w=200&text=http://www.ldstars.com/program/c/311.html"
                             alt="扫一扫在手机阅读"/>
                        <p>扫一扫 在手机阅读、分享本文</p>
                    </div>
                </div>

                <div class="article-pn">
                    <span class="preview col-md-6">上一篇：<a href="http://www.ldstars.com/program/c/307.html" rel="prev">C语言asctime_s函数固定格式输出当前时间</a></span>
                    <span class="next pull-right">下一篇：<a href="http://www.ldstars.com/hlw/312.html" rel="next">各大直播平台主播房间订阅号获取方法</a></span>
                </div>

                <div class="related">
                    <h3>相关文章</h3>
                    <ul class="row related-box">
                        <li class="col-sm-6">
                            <a href="http://www.ldstars.com/hlw/333.html" target="_blank" rel="bookmark"
                               title="解决WordPress更换主题后缩略图变形问题"><i class="fa fa-hand-o-right"></i>解决WordPress更换主题后缩略图变形问题</a>
                        </li>
                        <li class="col-sm-6">
                            <a href="http://www.ldstars.com/program/c/307.html" target="_blank" rel="bookmark"
                               title="C语言asctime_s函数固定格式输出当前时间"><i class="fa fa-hand-o-right"></i>C语言asctime_s函数固定格式输出当前时间</a>
                        </li>
                        <li class="col-sm-6">
                            <a href="http://www.ldstars.com/hlw/288.html" target="_blank" rel="bookmark"
                               title="echo回声离线音乐失效下载方法"><i class="fa fa-hand-o-right"></i>echo回声离线音乐失效下载方法</a>
                        </li>
                    </ul>
                </div>
                <div class="comments">
                    <div id="comments"><span style="font-weight:bold;font-size:14px;margin-right:30px;">全部回复</span><span
                            style="color: #f60;font-size:18px;font-weight: bold;">1</span>人评论<span
                            style="color: #f60;font-size:18px;margin-left:30px;font-weight: bold;">2,143</span>人参与
                    </div>
                    <!-- 评论列表-->
                    <div id="comment-lists">
                        <ol class="commentlist">
                            <li class="comment even thread-even depth-1" id="comment-266">
                                <div id="div-comment-266" class="comment-body">
                                    <div class="gravatar">
                                        <div class="comment-author vcard">
                                            <img alt=''
                                                 src='http://1.gravatar.com/avatar/7be328a5ca4ab3b22548786939efc064?s=50&#038;d=monsterid&#038;r=g'
                                                 srcset='http://1.gravatar.com/avatar/7be328a5ca4ab3b22548786939efc064?s=100&amp;d=monsterid&amp;r=g 2x'
                                                 class='avatar avatar-50 photo' height='50' width='50'/></div>
                                    </div>
                                    <div class="floor">1楼</div>
                                    <div class="commenttext">
                                        <span class="commentid"><span class="comment_author"><a
                                                href='http://www.gkwo.net/' rel='external nofollow' target='_blank'
                                                class='url'>工控资料窝</a></span><a data-hint="潜水" class="hint hint--top"><i
                                                class="fa fa-star yellow"></i></a></span>
                                        <div class="comment_text">
                                            <p>非常实用的分享，不错</p>
                                        </div>
                                        <div class="comment-info">
                                            <span class="datetime"><i
                                                    class="fa fa-clock-o"></i>2016-09-20 09:53:03</span>
                                            <span class="reply"><i class="fa fa-reply"></i><a rel='nofollow'
                                                                                              class='comment-reply-link'
                                                                                              href='http://www.ldstars.com/program/c/311.html?replytocom=266#respond'
                                                                                              onclick='return addComment.moveForm( "div-comment-266", "266", "respond", "311" )'
                                                                                              aria-label='回复给工控资料窝'>回复</a></span><span
                                                class="edit_comment"></span>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        </ol>
                        <div class="navigation">
                            <div id="comments-navi"></div>
                        </div>
                    </div>
                    <!-- //评论列表-->
                    <div id="respond">
                        <p>
                            <div id="respond" class="comment-respond">
                                <h3 id="reply-title" class="comment-reply-title">
                                    <small><a rel="nofollow" id="cancel-comment-reply-link"
                                              href="/program/c/311.html#respond" style="display:none;">【取消回复】</a>
                                    </small>
                                </h3>
                                <form action="http://www.ldstars.com/wp-comments-post.php" method="post"
                                      id="commentform" class="comment-form">
                        <p class="comment-notes"><span id="email-notes">电子邮件地址不会被公开。</span> 必填项已用<span class="required">*</span>标注
                        </p>
                        <p class="col-md-12 comments-form"><textarea id="comment" name="comment" tabindex="4" cols="40"
                                                                     rows="3" aria-required="true"></textarea></p><label
                            for="comment_mail_notify" style="display:none"><input type="checkbox"
                                                                                  name="comment_mail_notify"
                                                                                  id="comment_mail_notify"
                                                                                  value="comment_mail_notify"
                                                                                  checked="checked"/>有人回复时邮件通知我</label>
                        <div id="comment-author-info"><p class="comment-form-author col-md-4"><input name="author"
                                                                                                     type="text"
                                                                                                     placeholder="昵称：*"
                                                                                                     value=""
                                                                                                     size="30"/></p>
                            <p class="comment-form-email col-md-4"><input name="email" type="text" placeholder="邮箱：*"
                                                                          value="" size="30"/></p>
                            <p class="comment-form-url col-md-4"><input name="url" type="text" placeholder="网址" value=""
                                                                        size="30"/></p></div>
                        <p class="form-submit"><input name="submit" type="submit" id="submit" class="submit"
                                                      value="发表评论"/> <input type='hidden' name='comment_post_ID'
                                                                            value='311' id='comment_post_ID'/>
                            <input type='hidden' name='comment_parent' id='comment_parent' value='0'/>
                        </p>
                        <!--<p class="antispam-group antispam-group-q" style="clear: both;">-->
                            <!--<label>Current ye@r <span class="required">*</span></label>-->
                            <!--<input type="hidden" name="antspm-a" class="antispam-control antispam-control-a"-->
                                   <!--value="2017"/>-->
                            <!--<input type="text" name="antspm-q" class="antispam-control antispam-control-q" value="4.3"-->
                                   <!--autocomplete="off"/>-->
                        <!--</p>-->
                        <p class="antispam-group antispam-group-e" style="display: none;">
                            <label>Leave this field empty</label>
                            <input type="text" name="antspm-e-email-url-website"
                                   class="antispam-control antispam-control-e" value="" autocomplete="off"/>
                        </p>
                        </form>
                    </div><!-- #respond -->
                    <div id="loading-img"><img
                            src="http://www.ldstars.com/wp-content/themes/bulletin/img/loading_com.gif" alt="loading"/>
                    </div>
                    <div id="error-comments"></div>
                    <script type="text/javascript">    //快捷回复 Ctrl+Enter
                    //<![CDATA[
                    jQuery(document).keypress(function (e) {
                        if (e.ctrlKey && e.which == 13 || e.which == 10) {
                            jQuery(".submit").click();
                            document.body.focus();
                        } else if (e.shiftKey && e.which == 13 || e.which == 10) {
                            jQuery(".submit").click();
                        }
                    })
                    // ]]>
                    //<![CDATA[
                    var changeMsg = "[ 编辑信息 ]";
                    var closeMsg = "[ 隐藏信息 ]";
                    function toggleCommentAuthorInfo() {
                        jQuery('#comment-author-info').slideToggle('slow', function () {
                            if (jQuery('#comment-author-info').css('display') == 'none') {
                                jQuery('#toggle-comment-author-info').text(changeMsg);
                            } else {
                                jQuery('#toggle-comment-author-info').text(closeMsg);
                            }
                        });
                    }
                    //]]>
                    </script>
                </div>
        </div>
        </section>
        <!-- //Blog-Box -->
    </div>
    <!-- Widget-Box -->
    <div class="col-md-3 widget">
        <div class="widget-box">
            <div class="widget-item">
                <div class="about-author">
                    <div class="widget-author"><img alt=''
                                                    src='http://2.gravatar.com/avatar/b9cd8d7be31a51a09c61b2aca4786b06?s=80&#038;d=monsterid&#038;r=g'
                                                    srcset='http://2.gravatar.com/avatar/b9cd8d7be31a51a09c61b2aca4786b06?s=160&amp;d=monsterid&amp;r=g 2x'
                                                    class='avatar avatar-80 photo' height='80' width='80'/></div>
                    <div class="author-des"><h3 class="text-center">King</h3>
                        <p>律动星光-网站开发 技术交流群：483749829 最有价值的网站技术群！！！</p></div>
                </div>
            </div>
        </div>
        <div class="widget-box">
            <div class="widget-title"><h3>最新文章<span class="sub-title orange">New Articles</span></h3></div>
            <div class="widget-item">
                <div class="widget-post"><a href="http://www.ldstars.com/ios/358.html" target="_blank" rel="bookmark"
                                            title="给iPhone的信息添加多彩炫酷的动画效果"><img width="300" height="225"
                                                                               src="http://www.ldstars.com/wp-content/uploads/2016/08/945118c29cb261fc5453-300x225.jpg"
                                                                               class="attachment-thumbnail size-thumbnail wp-post-image"
                                                                               alt=""/><span>给iPhone的信息添加多彩炫酷的动画效果</span></a><span
                        class="btn-cat"><a href="http://www.ldstars.com/ios" title="苹果体验馆"">苹果体验馆</a></span></div>
                <div class="widget-post"><a href="http://www.ldstars.com/ios/355.html" target="_blank" rel="bookmark"
                                            title="苹果产品为什么选择锂离子电池？"><img width="300" height="225"
                                                                         src="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg"
                                                                         class="attachment-thumbnail size-thumbnail wp-post-image"
                                                                         alt=""
                                                                         srcset="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg 300w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-600x450.jpg 600w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52.jpg 640w"
                                                                         sizes="(max-width: 300px) 100vw, 300px"/><span>苹果产品为什么选择锂离子电池？</span></a><span
                        class="btn-cat"><a href="http://www.ldstars.com/ios" title="苹果体验馆"">苹果体验馆</a></span></div>
                <div class="widget-post"><a href="http://www.ldstars.com/program/arduino/353.html" target="_blank"
                                            rel="bookmark" title="arduino入门教程基础-模拟读取串口"><img width="300" height="225"
                                                                                             src="http://www.ldstars.com/wp-content/uploads/2017/02/75f5750f6dd6afbec57b-300x225.jpg"
                                                                                             class="attachment-thumbnail size-thumbnail wp-post-image"
                                                                                             alt=""/><span>arduino入门教程基础-模拟读取串口</span></a><span
                        class="btn-cat"><a href="http://www.ldstars.com/program/arduino"
                                           title="arduino"">arduino</a></span></div>
                <div class="widget-post"><a href="http://www.ldstars.com/ios/351.html" target="_blank" rel="bookmark"
                                            title="苹果官方 iPhone、iPad 和 iPod touch 电池保养小提示"><img width="300" height="225"
                                                                                               src="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg"
                                                                                               class="attachment-thumbnail size-thumbnail wp-post-image"
                                                                                               alt=""
                                                                                               srcset="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg 300w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-600x450.jpg 600w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52.jpg 640w"
                                                                                               sizes="(max-width: 300px) 100vw, 300px"/><span>苹果官方 iPhone、iPad 和 iPod touch 电池保养小提示</span></a><span
                        class="btn-cat"><a href="http://www.ldstars.com/ios" title="苹果体验馆"">苹果体验馆</a></span></div>
                <div class="widget-post"><a href="http://www.ldstars.com/ios/348.html" target="_blank" rel="bookmark"
                                            title="苹果官方延长电池使用时间和寿命方法"><img width="300" height="225"
                                                                           src="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg"
                                                                           class="attachment-thumbnail size-thumbnail wp-post-image"
                                                                           alt=""
                                                                           srcset="http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-300x225.jpg 300w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52-600x450.jpg 600w, http://www.ldstars.com/wp-content/uploads/2017/02/25b2916b5c49db617f52.jpg 640w"
                                                                           sizes="(max-width: 300px) 100vw, 300px"/><span>苹果官方延长电池使用时间和寿命方法</span></a><span
                        class="btn-cat"><a href="http://www.ldstars.com/ios" title="苹果体验馆"">苹果体验馆</a></span></div>
                </ul></div>
        </div>
    </div>
    <!-- //Widget-Box -->
</div>
</div>
{include file="Public/footer"/}

<!-- Back-To-Top -->
<div class="top">
    <a href="javascript:void(0);" id="back-to-top"><i class="fa fa-arrow-up fa-3x"></i></a>
</div>
<!-- //Back-To-Top -->
<script type='text/javascript'>
    /* <![CDATA[ */
    var ajaxcomment = {
        "ajax_url": "http:\/\/www.ldstars.com\/wp-admin\/admin-ajax.php",
        "order": "desc",
        "formpostion": "bottom"
    };
    /* ]]> */
</script>
<script type="text/javascript">
    (function () {
        document.write(unescape('%3Cdiv id="bdcs"%3E%3C/div%3E'));
        var bdcs = document.createElement('script');
        bdcs.type = 'text/javascript';
        bdcs.async = true;
        bdcs.src = 'http://znsv.baidu.com/customer_search/api/js?sid=12844460128595975264' + '&plate_url=' + encodeURIComponent(window.location.href) + '&t=' + Math.ceil(new Date() / 3600000);
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(bdcs, s);
    })();
</script>
<script type="text/javascript">
    $('#award').jBox('Modal', {
        title: '感谢您的支持，我会继续努力的!',
        content: $('.award_box'),
        animation: 'pulse',
    });

    $(".pay_item").click(function () {
        $(this).addClass('checked').siblings('.pay_item').removeClass('checked');
        var dataid = $(this).attr('data-id');
        $(".award_payimg img").attr("src", "http://www.ldstars.com/wp-content/themes/bulletin/img/" + dataid + "img.jpg");
        $("#award_pay_txt").text(dataid == "alipay" ? "支付宝" : "微信");
    });
    new jBox('Image');
</script>